package com.unionpay.upyzt.example;

import com.unionpay.upyzt.Upyzt;
import com.unionpay.upyzt.exception.UpyztException;
import com.unionpay.upyzt.resp.MchApplicationResp;
import com.unionpay.upyzt.resp.PaymentResp;
import com.unionpay.upyzt.util.StringUtils;

import java.io.IOException;
import java.net.URL;

public class Main {

    private final static String rootMchId = "1002512373299546172";

    private final static String signPrivateKeyFilePath = "your_sign_private_key.pem";

    private final static String encryptKeyFilePath = "your_encrypt_public_key.pem";

    private final static String verifyKeyFilePath = "your_verify_public_key.pem";

    public static void main(String[] args) throws Exception {
        // 初始化配置
        initConfig();

        try {
            // 图片上传 Demo
            MediaExample.runDemos();

            // 设定运行 Demo 所需要的 ID，可替换为自身系统使用的 ID 进行测试
            String cusId = "1102533851436681293";
            String mchId = null;
            String payBalanceAcctId = "2002541740929124424";
            String recvBalanceAcctId = "2002512535974905940";

            // 个人用户进件 Demo
//            CusApplicationResp cusApplicationResp = CusApplicationExample.runDemos();
//            cusId = cusApplicationResp.getCusId();
//            payBalanceAcctId = cusApplicationResp.getBalanceAcctId();

            // 二级商户进件 Demo
            MchApplicationResp mchApplicationResp = MchApplicationExample.runDemos();
            mchId = mchApplicationResp.getMchId();

            // 绑定账户 Demo
//            SettleAcctExample.runDemos(cusId, mchId);

            // 交易授权码 Demo
//            AcctPasswordExample.runDemos(payBalanceAcctId);

            // 电子账簿 Demo
            BalanceAcctExample.runDemos(cusId, mchId);

            // 充值支付 Demo
            DepositExample.runDemos(payBalanceAcctId);

            // 提现 Demo（需等待入账后余额变为可提现）
            WithdrawalExample.runDemos(payBalanceAcctId);

            // 批量提现
            WithdrawalApplicationExample.runDemos(payBalanceAcctId);

            // 分账 Demo（需等待入账后余额变为可提现）
            AllocationExample.runDemos(payBalanceAcctId, recvBalanceAcctId);

            // 消费（转账） Demo
            PaymentResp paymentResp = PaymentExample.runDemos(payBalanceAcctId, recvBalanceAcctId);

            // 电子回单
//            ElectronicReceiptExample.runDemos(paymentResp.getPaymentId(), paymentResp.getOutOrderNo(), "payment");

            // 母户入金、转账入金、大额订单入金列表查询
            AcctDepositExample.runDemos(recvBalanceAcctId);

            // 可提现余额冻结/解冻
            BalanceFreezeExample.runDemos(recvBalanceAcctId);

            // 大额订单
            LargePaymentExample.runDemos(recvBalanceAcctId);

            // 转账入金退款
            TransferRefundExample.runDemos();

            // 可提现充值
            WithdrawableDepositExample.runDemos(recvBalanceAcctId);

            // 担保下单 Demo
            String guaranteePaymentId = GuaranteePaymentExample.runDemos(payBalanceAcctId, recvBalanceAcctId);

            // 担保确认 Demo
            String guaranteeConfirmId = GuaranteeConfirmExample.runDemos(guaranteePaymentId);

            // 退款 Demo
            RefundExample.runDemos(paymentResp.getPaymentId());
            RefundExample.runDemos(guaranteePaymentId);
            RefundExample.runDemos(guaranteeConfirmId);

            // 合并充值支付 Demo  ,合并支付充值 是 合并充值的批量接口
            CombinedDepositExample.runDemos(payBalanceAcctId);

            // 合并消费担保下单 Demo。合并消费担保下单 是 担保消费的批量接口，
            // 注意返回的是子单ID
            String guaranteePaymentId1 = CombinedGuaranteePaymentExample.runDemos(payBalanceAcctId, recvBalanceAcctId);

            // 合并消费担保下单子单退款
            RefundExample.runDemos(guaranteePaymentId1);

            // 合并消费担保确认 Demo。合并消费担保确认，是对合并消费担保下单 子单 的 确认
            String combinedGuaranteeConfirmId = CombinedGuaranteeConfirmExample.runDemos(guaranteePaymentId1);

            // 合并消费担保确认退款，是对合并消费担保确认订单的退款
            RefundExample.runDemos(combinedGuaranteeConfirmId);

            // 合并担保支付
            String guaranteeDepositId = CombinedGuaranteeDepositExample.runDemos(payBalanceAcctId);

            // 合并担保支付确认
            CombinedDepositConfirmExample.runDemos(guaranteeDepositId);

            // 批量消费
            BatchPaymentExample.runDemos(payBalanceAcctId, recvBalanceAcctId);

            // 申请交易账单 Demo（对账单下载必须交易对账完成（一般为 T+1 日））
            // TradeBillExample.runDemos(LocalDate.now().minusDays(1));

            // 电子回单（电子回单对应的交易必须交易成功且对账完成（一般为 T+1 日））
//            ElectronicReceiptExample.runDemos(paymentResp.getPaymentId(), paymentResp.getOutOrderNo(), "payment");

        } catch (UpyztException e) {
            e.printStackTrace();
        }
    }

    private static void initConfig() throws IOException {
        // 设定为测试环境
        Upyzt.setApiBase(Upyzt.TEST_API_BASE);

        // （必填）设置平台商户 Id
        Upyzt.setRootMchId(rootMchId);

        // （必填）设置签名私钥路径，用于请求签名
        Upyzt.setSignPrivateKeyPath(getResPath(signPrivateKeyFilePath));

        // （必填）设置加密公钥路径，用于敏感字段加密
        Upyzt.setEncryptPublicKeyPath(getResPath(encryptKeyFilePath));

        // （可选，推荐设置）设置响应验证公钥，用于验证响应是否真实有效；不设置或设置密钥错误将不验签
        Upyzt.setVerifyPublicKeyPath(getResPath(verifyKeyFilePath));
    }

    public static String generateRandomOutNo() {
        return "sdk-example-" + StringUtils.currentTimeStamp() + StringUtils.randomAlphanumeric(5);
    }

    public static String getResPath(String subPath) {
        if (subPath == null) {
            throw new NullPointerException();
        }
        URL resource = Thread.currentThread().getContextClassLoader().getResource(subPath);
        return resource != null ? resource.getPath() : subPath;
    }
}
